// pages/keepcar/index.js
var util = require('../../utils/util.js');
const app = getApp();
var currentTime, interval;
var clickflag = 1;
var submitflag = 1;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        listInfo: [],
        showTopTips: false,
        errorMsg: "",
        date:'2019-08-05',
        bannerUrl:'',
        radioItems: [
            { name: '开车到店', value: '0', checked: true },
            { name: '派人取车', value: '1' }
        ],
        getcartype:0,
        submitname: '提交保养',
        time: '获取验证码', //倒计时 
        currentTime: 61,
        mobile: '',
        isshow:true,
        showModalAuth: false,
        animationData: '',
        isLoginAuth: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var date = new Date();
        var dateStr = util.formatDate(date);
        if (!app.globalData.isLoginAuth) {
          this.showModal();
        }
        this.setData({
            date: dateStr,
            isLoginAuth: app.globalData.isLoginAuth
        })
        
    },
    //广告图片初始化
    initBanner: function () {
        var that = this;
        
        var requrl = app.globalData.hostUrl + 'common/banner';
        var imgbanner = wx.getStorageSync('keepBanner');
        //console.log(imgbanner.length);
        if (!app.expiratTime('keepBanner') || imgbanner.length == 0) {
            wx.request({
                url: requrl,
                data: { 'location': '302' },
                header: {
                    'grant-appid': app.globalData.appid
                },
                success: function (res) {
                    if (res.statusCode === 200) {
                        wx.setStorageSync('keepBanner', res.data.result[0].imgpath);
                        that.setData({
                            bannerUrl: res.data.result[0].imgpath
                        })
                    }
                }
            })
        }
        else {
            that.setData({
                bannerUrl: imgbanner
            })
        }
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
          listInfo: app.globalData.listInfo
        })
        this.initBanner();
    },
    radioChange: function (e) {
        console.log('radio发生change事件，携带value值为：', e.detail.value);

        var radioItems = this.data.radioItems;
        for (var i = 0, len = radioItems.length; i < len; ++i) {
            radioItems[i].checked = radioItems[i].value == e.detail.value;
        }

        this.setData({
            radioItems: radioItems,
            getcartype: e.detail.value
        });

    },
    bindDateChange: function (e) {
        this.setData({
            date: e.detail.value
        })
    },
    changeVal: function (e) {
        var that = this;
        this.setData({
            mobile: e.detail.value
        })
        util.clearError(that);
    },
    //获取手机验证码
    getSncode: function () {
        var that = this;
        var phone = this.data.mobile;
        var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
        if (phone == '') {
            util.isError("手机号不为空!", that);
            return false;
        } else if (!myreg.test(phone)) {
            util.isError("请输入正确的手机号!", that);
            return false;
        }

        var requrl = app.globalData.hostUrl + 'common/sendsms';
        if (clickflag == 1) {
            util.clearError(that);
            clickflag = 0;
            currentTime = that.data.currentTime
            interval = setInterval(function () {
                currentTime--;
                that.setData({
                    time: currentTime + '秒'
                })
                if (currentTime <= 0) {
                    clearInterval(interval)
                    that.setData({
                        time: '重新发送',
                        currentTime: 61,
                    })
                    clickflag = 1;
                }
            }, 1000);

            wx.request({
                url: requrl,
                data: {
                    'phone': phone
                },
                header: {
                    'grant-appid': app.globalData.appid
                },
                success: function (res) {
                    console.log(res);
                    if (res.data.code == 200) {
                        wx.showToast({
                            title: '验证码已发送!',
                            icon: 'none',
                            duration: 2000
                        })
                    } else {
                        util.isError('获取失败,' + res.data.result, that);
                        return;
                    }
                    clickflag = 1;
                }
            })
        }
    },
    clearErrtip: function () {
        var that = this;
        util.clearError(that);
    },
    showModal: function () {
      // 显示遮罩层
      var animation = wx.createAnimation({
        duration: 200,
        timingFunction: "ease-in-out",
        delay: 0
      })
      this.animation = animation
      animation.translateY(500).step()
      this.setData({
        animationData: animation.export(),
        showModalAuth: true
      })
      setTimeout(function () {
        animation.translateY(0).step()
        this.setData({
          animationData: animation.export()
        })
      }.bind(this), 200)
    },
    hideModal: function () {
      this.setData({
        showModalAuth: false,
      })
    },
    bindGetUserInfo: function (e) {
      console.log(e);
      if (e.detail.errMsg == 'getUserInfo:ok') {
        app.globalData.userInfo = e.detail.userInfo;
        app.getfansinfo();
      }
      else if (e.detail.errMsg == "getUserInfo:fail auth deny") {

      }
      else {
        wx.redirectTo({
          url: '../keepcar/review',
        })
      }
    },
    formSubmit: function (e) {
        var that = this;
        var name = e.detail.value.name;
        var code = e.detail.value.code;
        var keeptime = e.detail.value.keeptime;
        var addr = e.detail.value.address;
        var phone = this.data.mobile;
        var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
        if (phone == '') {
            util.isError("手机号不为空!", that);
            return false;
        } else if (!myreg.test(phone)) {
            util.isError("请输入正确的手机号!", that);
            return false;
        }
        if (name == '') {
            util.isError("姓名不为空!", that);
            return false;
        }
        if (code == '') {
            util.isError("验证码不为空!", that);
            return false;
        }
        if (this.data.getcartype==1 && addr==''){
            util.isError("请填写取车地址!", that);
            return false;
        }

        var postdata = {
            'name': name,
            'code': code,
            'mobile': phone,
            'kptime': keeptime,
            'getcar': this.data.getcartype,
            'addr': addr,
            'openid': wx.getStorageSync('openId'),
            'userid': wx.getStorageSync('userId')
        }

        console.log(postdata);
        var requrl = app.globalData.hostUrl + 'handler/kpcarOrder';
        if(submitflag==1){
            wx.request({
                url: requrl,
                data: postdata,
                header: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'grant-appid': app.globalData.appid
                },
                method: 'POST',
                success: function (res) {
                    //console.log(res);
                    if (res.data.code == 200) {
                        wx.showModal({
                            title: '提示',
                            content: '您的订单已提交，请等待客服与您联系确认!',
                            showCancel: false,
                            success(res) {
                                if (res.confirm) {
                                  wx.navigateTo({
                                        url: '/pages/index/index',
                                    })
                                } else if (res.cancel) {
                                   
                                }
                            }
                        })
                    }
                    else {
                        util.isError(res.data.result, that);
                        submitflag == 0
                    }
                }
            })
        }
    },
  calling: function (e) {
    var mobile = '15916351997';
    wx.makePhoneCall({
      phoneNumber: mobile,
      success: function () {
        //console.log("拨打电话成功！")
      },
      fail: function () {
        // console.log("拨打电话失败！")
      }
    })
  },
  gohome: function () {
    wx.redirectTo({
      url: '../index/index',
    })
  },
  yuy: function () {
    if (1 == wx.getStorageSync('roleidentity')) {
      wx.redirectTo({
        url: '../myself/thappointment',
      })
    }
    else {
      wx.redirectTo({
        url: '../myself/myappointment',
      })
    }
  },
  orders: function () {
    if (1 == wx.getStorageSync('roleidentity')) {
      wx.redirectTo({
        url: '../myself/trorder',
      })
    }
    else {
      wx.redirectTo({
        url: '../myself/myorder',
      })
    }
  },
  mys: function () {
    wx.redirectTo({
      url: '../myself/myself',
    })
  },
})